package com.simulation.model;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SimulationTask {
    private String taskId;
    private String templateId;
    private String taskName;
    private TaskStatus status;
    private SimulationMode mode; // NORMAL or ABNORMAL
    private LocalDateTime startTime;
    private LocalDateTime lastUpdateTime;
    private long runDuration; // in milliseconds
    private Map<String, Object> currentValues; // 当前数据点值
    private SimulationTemplate template;
    private Thread workerThread;

    public SimulationTask() {
        this.currentValues = new ConcurrentHashMap<>();
        this.status = TaskStatus.CREATED;
        this.mode = SimulationMode.NORMAL;
        this.startTime = LocalDateTime.now();
        this.lastUpdateTime = LocalDateTime.now();
    }

    public SimulationTask(String taskId, String templateId, String taskName, SimulationTemplate template) {
        this();
        this.taskId = taskId;
        this.templateId = templateId;
        this.taskName = taskName;
        this.template = template;
    }

    // Getters and Setters
    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public TaskStatus getStatus() {
        return status;
    }

    public void setStatus(TaskStatus status) {
        this.status = status;
        this.lastUpdateTime = LocalDateTime.now();
    }

    public SimulationMode getMode() {
        return mode;
    }

    public void setMode(SimulationMode mode) {
        this.mode = mode;
        this.lastUpdateTime = LocalDateTime.now();
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public LocalDateTime getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(LocalDateTime lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    public long getRunDuration() {
        return runDuration;
    }

    public void setRunDuration(long runDuration) {
        this.runDuration = runDuration;
    }

    public Map<String, Object> getCurrentValues() {
        return currentValues;
    }

    public void setCurrentValues(Map<String, Object> currentValues) {
        this.currentValues = currentValues;
    }

    public SimulationTemplate getTemplate() {
        return template;
    }

    public void setTemplate(SimulationTemplate template) {
        this.template = template;
    }

    public Thread getWorkerThread() {
        return workerThread;
    }

    public void setWorkerThread(Thread workerThread) {
        this.workerThread = workerThread;
    }

    public void updateValue(String pointName, Object value) {
        this.currentValues.put(pointName, value);
        this.lastUpdateTime = LocalDateTime.now();
    }

    public Object getValue(String pointName) {
        return this.currentValues.get(pointName);
    }

    public enum TaskStatus {
        CREATED,    // 已创建
        RUNNING,    // 运行中
        STOPPED,    // 已停止
        ERROR,      // 错误状态
        PAUSED      // 暂停
    }

    public enum SimulationMode {
        NORMAL,     // 正常模拟
        ABNORMAL    // 异常模拟
    }
} 